package com.sunday.android.fangdaifu.bukong.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.activity.index.DoOptionActivity;
import com.sunday.android.fangdaifu.bukong.activity.index.StartOptionActivity;
import com.sunday.android.fangdaifu.bukong.entity.BindBack;
import com.sunday.android.fangdaifu.bukong.entity.InstallEquipment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/7/20.
 */

public class EquipInnerAdapter extends BaseAdapter {

    private Context context;
    private List<InstallEquipment.ListBean> list = new ArrayList<>();

    public EquipInnerAdapter( Context context,List<InstallEquipment.ListBean> list){
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (holder==null){
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.equip_view_item,parent,false);
            holder.name = ButterKnife.findById(convertView,R.id.tvName);
            holder.type = ButterKnife.findById(convertView,R.id.tvType);
            holder.option = ButterKnife.findById(convertView,R.id.tvOption);
            holder.view = ButterKnife.findById(convertView,R.id.lMain);
            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
        }
        final InstallEquipment.ListBean bean = list.get(position);
        if (bean.getStatus()==0){
            holder.option.setSelected(true);
            holder.option.setText("选择设备");
        } else if (bean.getStatus()==1){
            holder.option.setSelected(false);
            holder.option.setText("已匹配");
        }else {
            holder.option.setSelected(false);
            holder.option.setText("已完成");
        }
        if (bean.getType()==1){
            holder.type.setText("倾斜测量设备");
        }
        if (bean.getType()==2){
            holder.type.setText("沉降测量设备");
        }
        if (bean.getType()==3){
            holder.type.setText("裂缝(位移)测量设备");
        }
        holder.name.setText(bean.getPointNo());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.getStatus()==0){
                    Intent intent = new Intent(context, StartOptionActivity.class);
                    intent.putExtra("id",bean.getId());
                    context.startActivity(intent);
                }else {
                    Intent intent = new Intent(context, DoOptionActivity.class);
                    intent.putExtra("id",bean.getId());
                    intent.putExtra("plantId",bean.getPlantId());
                    BindBack bindBack = new BindBack();
                    bindBack.setImageList(bean.getImageList());
                    bindBack.setNowValue(bean.getNowValue());
                    bindBack.setPointNo(bean.getPointNo());
                    bindBack.setPlantNo(bean.getPlantNo());
                    bindBack.setValue(bean.getValue());
                    bindBack.setType(bean.getType());
                    bindBack.setStatus(bean.getStatus());
                    bindBack.setData(bean.getData());
                    bindBack.setNs_data(bean.getNs_data());
                    bindBack.setEw_data(bean.getEw_data());
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("bind",bindBack);
                    intent.putExtra("bind",bundle);
                    intent.putExtra("isFinish",true);
                    context.startActivity(intent);
                }
            }
        });
        return convertView;
    }

    class Holder {
        public TextView name,type,option;
        public View view;
    }
}
